<?php
/**
 * 个人会员中心   评分系统类
 * @author      HJC <hujianchuang@haowj.com>
 * @update      2014.07.01
 * @since       Version 1.0
 * 
 */
class GradeAction extends BaseAction 
{
    public function index()
    {
        $user_id = $this->userInfo['id'];
        $res = D('Grade')->where("user_id=".$user_id)->order("id desc")->select();
        foreach ($res as $k=>$v)
        {
            if($v['type']==C('PACKCLASSTYPE'))
            {
                $new_course_id = D('New_classroom')->where("id=".$v['cid'])->getField('new_course_id');
                $res[$k]['lesson_title'] = D('New_course_lesson')->where("course_id=".$new_course_id." and lesson_sort=".$v['lesson_id'])->getField('lesson_title');
            }
        }
        $lesson_info  = D('Lessoninfo')->select();
        $this->assign('lesson_info',$lesson_info);
        $this->assign('res',$res);
        $seo['title'] = '评分管理_好外教网'; 
       
        $this->assign('seo',$seo);
        $this->display();
    }
    //给老师评分
    public function doGrade()
    {
        $user_id = $this->userInfo['id'];
        $id = $this->_get('id');
        //判断是否评价过
        $grade = D('Grade')->where("id=".$id." and user_id=".$user_id)->find();
        if(!$id||!$grade) $false="课程还没开始上或数据错误！";
        if($grade['flag']==1) $false="您已经做过评价啦！";
        if($grade['type']==C('PACKCLASSTYPE'))
        {
            $classInfo = D('New_classroom')->where("id=".$grade['cid'])->find();
            if(!$classInfo) $false="课程不存在！";
        }
        $this->assign('grade',$grade);
        $this->assign('classInfo',$classInfo);
        $this->assign('false',$false);
        $this->display();
    }
    //提交评论
    function postGrade()
    {
        $user_id = $this->userInfo['id'];
        if(!IS_AJAX) $this->ajaxReturn('','非法请求！',0); 
        $post = $this->_post();
        $post['teacher_score'] = $post['interaction']+$post['interest']+$post['pronun']+$post['explain']+$post['rhythm'];
        $grade = D('Grade')->where("id=".$post['id']." and cid=".$post['cid']." and user_id=".$user_id." and type=".$post['type'])->find();
        if(!$grade) $this->ajaxReturn('','课程还没有开始 或者数据错误！',0); 
        if($grade['flag']==1) $this->ajaxReturn('','您已经评价过了！',0); 
        $post['flag'] = 1;
        D('Grade')->where("id=".$post['id']." and cid=".$post['cid']." and user_id=".$user_id." and type=".$post['type'])->data($post)->save();
        $this->ajaxReturn('','评价成功！',1); 
    }
}